<template>
    <div>
        <div>
            <el-table :data="emps" border stripe size="mini">
                <el-table-column type="selection" align="left" width="55"></el-table-column>
                <el-table-column prop="name" label="姓名" fixed width="120" align="left"></el-table-column>
                <el-table-column prop="workID" label="工号" width="120" align="left"></el-table-column>
                <el-table-column prop="email" label="电子邮件" width="200" align="left"></el-table-column>
                <el-table-column prop="phone" label="电话号码" width="120" align="left"></el-table-column>
                <el-table-column prop="department.name" label="所属部门" width="120" align="left"></el-table-column>
                <el-table-column label="详情" align="center">
                    <template slot-scope="scope">
                        <el-tooltip placement="right" v-if="scope.row.salary">
                            <div slot="content">
                                <table>
                                    <tr>
                                        <td>基本工资</td>
                                        <td>{{scope.row.salary.basicSalary}}</td>
                                    </tr>
                                    <tr>
                                        <td>交通补助</td>
                                        <td>{{scope.row.salary.trafficSalary}}</td>
                                    </tr>
                                    <tr>
                                        <td>午餐补助</td>
                                        <td>{{scope.row.salary.lunchSalary}}</td>
                                    </tr>
                                    <tr>
                                        <td>奖金</td>
                                        <td>{{scope.row.salary.bonus}}</td>
                                    </tr>
                                    <tr>
                                        <td>养老金比率</td>
                                        <td>{{scope.row.salary.pensionPer}}</td>
                                    </tr>
                                    <tr>
                                        <td>养老金基数</td>
                                        <td>{{scope.row.salary.pensionBase}}</td>
                                    </tr>
                                    <tr>
                                        <td>医疗保险比率</td>
                                        <td>{{scope.row.salary.medicalPer}}</td>
                                    </tr>
                                    <tr>
                                        <td>医疗保险基数</td>
                                        <td>{{scope.row.salary.medicalBase}}</td>
                                    </tr>
                                    <tr>
                                        <td>公积金比率</td>
                                        <td>{{scope.row.salary.accumulationFundPer}}</td>
                                    </tr>
                                    <tr>
                                        <td>公积金基数</td>
                                        <td>{{scope.row.salary.accumulationFundBase}}</td>
                                    </tr>
                                    <!-- <tr>
                                        <td>启用时间</td>
                                        <td>{{scope.row.salary.createDate}}</td>
                                    </tr> -->
                                </table>
                            </div>
                            <el-tag>工资详情</el-tag>
                        </el-tooltip>
                        <el-tag v-else>暂未设置</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <!-- <el-popover
                                placement="left"
                                title="修改工资"
                                @show="showPop(scope.row.salary)"
                                @hide="hidePop(scope.row)"
                                width="200"
                                trigger="click">
                            <div>
                                <el-select v-model="currentSalary" placeholder="请选择" size="mini">
                                    <el-option
                                            v-for="item in salaries"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.id">
                                    </el-option>
                                </el-select>
                            </div>
                            <el-button slot="reference" type="danger">修改工资</el-button>
                        </el-popover> -->
                        <el-button type="danger" @click="exitSalarie(scope.row.salary, scope.row)">修改工资</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @size-change="sizeChange"
                        @current-change="currentChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>

            <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" >
                <div>
                    <el-form :model="salarie" ref="salarieForm">
                        <el-row>
                            <el-col span="8">
                                <el-form-item label="基本工资:" prop="basicSalary">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.basicSalary"
                                        placeholder=" 请输入基本工资"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col span="8">
                                <el-form-item label="奖金:" prop="bonus">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.bonus"
                                        placeholder=" 请输入奖金"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col span="8">
                                <el-form-item label="交通补助:" prop="trafficSalary">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.trafficSalary"
                                        placeholder=" 请输入交通补助"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col span="8">
                                <el-form-item label="午餐补助:" prop="lunchSalary">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.lunchSalary"
                                        placeholder=" 请输入午餐补助"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col span="8">
                                <el-form-item label="养老金比率:" prop="pensionPer">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.pensionPer"
                                        placeholder=" 请输入养老金比率"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col span="8">
                                <el-form-item label="养老金基数:" prop="pensionBase">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.pensionBase" 
                                        placeholder="请输入养老金基数"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                           <el-col span="8">
                                <el-form-item label="医疗保险比率:" prop="medicalPer">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.medicalPer" 
                                        placeholder="请输入医疗保险比率"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col span="8">
                                <el-form-item label="医疗保险基数:" prop="medicalBase">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.medicalBase" 
                                        placeholder="请输入医疗保险基数"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col span="8">
                                <el-form-item label="公积金比率:" prop="accumulationFundPer">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.accumulationFundPer" 
                                        placeholder="请输入公积金比率"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col span="8">
                                <el-form-item label="公积金基数:" prop="accumulationFundBase">
                                    <el-input size="mini" style="width: 100px" v-model="salarie.accumulationFundBase" 
                                        placeholder="请输入公积金基数"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
                <span slot="footer" class="dialog-footer">
                    
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAddSalarie">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>

</template>

<script>
    export default {
        name: "SalEmp",
        data() {
            return {
                emps: [],
                total: 0,
                currentPage: 1,
                currentSize: 10,
                currentSalary: null,
                salaries: [],

                dialogVisible: false,
                title: '',
                salarie:{}
            }
        },
        mounted() {
            this.initEmps();
            this.initSalaries();
        },
        methods: {
            sizeChange(size) {
                this.currentSize = size;
                this.initEmps();
            },
            currentChange(page) {
                this.currentPage = page;
                this.initEmps();
            },
            hidePop(data) {
                if (this.currentSalary) {
                    this.putRequest('/salary/emp/?eid=' + data.id + '&sid=' + this.currentSalary).then(resp => {
                        if (resp) {
                            this.initEmps()
                        }
                    });
                }
            },
            showPop(data) {
                if (data) {
                    this.currentSalary = data.id;
                } else {
                    this.currentSalary = null;
                }
            },
            initSalaries() {
                this.getRequest("/salary/emp/salaries").then(resp => {
                    if (resp) {
                        this.salaries = resp;
                    }
                })
            },
            initEmps() {
                this.getRequest("/salary/emp/?page=" + this.currentPage + '&size=' + this.currentSize).then(resp => {
                    if (resp) {
                        this.emps = resp.data;
                        this.total = resp.total;
                    }
                })
            },
            exitSalarie(data, emp) {
                console.log(data, emp)
                this.dialogVisible = true;
                this.salarie = data;
                this.salarie.eId = emp.id;
                this.title = '修改工资'
            },
            doAddSalarie() {
                console.log(this.salarie)
                if (this.salarie.eId) {
                    this.$refs['salarieForm'].validate(valid => {
                        if (valid) {
                            this.putRequest("/salary/emp/", this.salarie).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initEmps();
                                }
                            })
                        }
                    });
                } else {
                    this.$refs['salarieForm'].validate(valid => {
                        if (valid) {
                            this.postRequest("/salary/emp/", this.salarie).then(resp => {
                                if (resp) {
                                    this.dialogVisible = false;
                                    this.initEmps();
                                }
                            })
                        }
                    });
                }
            }
        }
    }
</script>

<style scoped>

</style>